package com.lj.page;

import com.lj.dao.ParamsInfoDao;
import com.lj.dao.SqliteHelper;
import com.lj.domain.ParamsInfo;
import com.lj.domain.SimpleResponse;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by dave_suwei on 2017/12/7.
 */
public class ParamsUpdateServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding( "UTF-8" );

        SimpleResponse sr=new SimpleResponse();
        sr.setOk(true);
        sr.setMsg("成功!");
        //取参数
        long showTime=0;
        try{showTime=Long.parseLong(req.getParameter("showTime"));}catch (NumberFormatException ex){}

        int lastSelect=0;
        try{lastSelect=Integer.parseInt(req.getParameter("lastSelect"));}catch (NumberFormatException ex){}

        int showCount=0;
        try{showCount=Integer.parseInt(req.getParameter("showCount"));}catch (NumberFormatException ex){}

        int rejectCount=0;
        try{rejectCount=Integer.parseInt(req.getParameter("rejectCount"));}catch (NumberFormatException ex){}

        int considerCount=0;
        try{considerCount=Integer.parseInt(req.getParameter("considerCount"));}catch (NumberFormatException ex){}

        System.out.println(showTime+","+lastSelect+","+showCount+","+rejectCount+","+considerCount);
        ParamsInfo paramsInfo=new ParamsInfo();
        paramsInfo.setShowTime(showTime);
        paramsInfo.setLastSelect(lastSelect);
        paramsInfo.setShowCount(showCount);
        paramsInfo.setRejectCount(rejectCount);
        paramsInfo.setConsiderCount(considerCount);

        SqliteHelper sh= (SqliteHelper) req.getAttribute("sh");
        if(sh!=null) {
            ParamsInfoDao paramsInfoDao = new ParamsInfoDao(sh);
            sr.setOk(paramsInfoDao.setParams(paramsInfo));
        }else{
            sr.setOk(false);
            sr.setMsg("!");
        }

        resp.setContentType("application/json");
        resp.setCharacterEncoding("UTF-8");
        resp.getWriter().println(sr.toJson());
    }
}
